package com.mk.admin.service.impl;

import com.mk.admin.mapper.PatientGuidanceMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.entity.PatientGuidanceEntity;
import com.mk.admin.model.vo.PatientGuidanceBatchVo;
import com.mk.admin.model.vo.PatientGuidanceVo;
import com.mk.admin.service.PatientGuidanceService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.JSONCopy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PatientGuidanceServiceImpl implements PatientGuidanceService {
    @Autowired
    private PatientGuidanceMapper patientGuidanceMapper;

    @Override
    public ResultDto select(PatientGuidanceVo patientGuidanceVo) {
        PatientGuidanceEntity entity = JSONCopy.copy(patientGuidanceVo, PatientGuidanceEntity.class);
        return ResultDto.data(patientGuidanceMapper.count(entity), patientGuidanceMapper.select(entity));
    }

    @Override
    public ResultDto add(PatientGuidanceVo patientGuidanceVo) {
        PatientGuidanceEntity entity = JSONCopy.copy(patientGuidanceVo, PatientGuidanceEntity.class);
        entity.setSignTime(DateTool.format());
        return ResultDto.data(patientGuidanceMapper.insert(entity));
    }

    @Override
    public ResultDto update(PatientGuidanceVo patientGuidanceVo) {
        PatientGuidanceEntity entity = JSONCopy.copy(patientGuidanceVo, PatientGuidanceEntity.class);
        return ResultDto.data(patientGuidanceMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(patientGuidanceMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(patientGuidanceMapper.status(id));
    }

    @Override
    public ResultDto deleteBatchIds(List<PatientGuidanceVo> list) {
        List<Integer> ids = list.stream().map(PatientGuidanceVo::getId).collect(Collectors.toList());
        return ResultDto.data(patientGuidanceMapper.deleteBatchIds(ids));
    }

    @Override
    public ResultDto addBatch(PatientGuidanceBatchVo vo) {
        List<PatientGuidanceVo> list = vo.getList();
//        for (PatientGuidanceVo patientGuidanceVo : list) {
//            PatientGuidanceEntity entity = JSONCopy.copy(patientGuidanceVo, PatientGuidanceEntity.class);
//            patientGuidanceMapper.insert(entity);
//        }
        List<PatientGuidanceEntity> entityList = JSONCopy.copy(list, PatientGuidanceEntity.class);
        return ResultDto.data(patientGuidanceMapper.addBatch(entityList));
    }
}
